package com.bds.tjj.snatch;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.DaoException;
import org.nutz.dao.Sqls;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bds.tjj.http.HttpBase;
import com.bds.tjj.model.MonthData;
import com.bds.tjj.model.PropIndex;
import com.bds.tjj.utils.SpringContextUtil;

/**
 * 按月抓取
 */

public class StatsMouthThread extends Thread {

	private NutDao dao = SpringContextUtil.getBean("dao", NutDao.class);

	private static Logger logger = Logger.getLogger(StatsMouthThread.class);

	// 获取任务列表
	public static List<String> demoSql(NutDao dao, final String name1) {
		Sql sql = Sqls.create("SELECT DISTINCT " + name1 + " from T_PROP_INDEX where status=1 and chart_type='month'");
		// sql.params().set("name", "A%");
		sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<String> list = new LinkedList<String>();
				while (rs.next())
					list.add(rs.getString(name1));
				return list;
			}
		});
		dao.execute(sql);
		return sql.getList(String.class);
	}

	@Override
	public void run() {

		try {
			this.startCrawl();
		} catch (Exception e) {
			logger.error("", e);
		}
	}
	public void initCrawl() throws Exception {
		List<String> cn_names = demoSql(dao, "cn_name");
		
		this.snatchData("A1501", dao, cn_names);
		this.snatchData("A150501", dao, cn_names);
		this.snatchData("A150502", dao, cn_names);
		this.snatchData("A150503", dao, cn_names);
		this.snatchData("A150504", dao, cn_names);
		this.snatchData("A150505", dao, cn_names);
		this.snatchData("A150506", dao, cn_names);
		this.snatchData("A150508", dao, cn_names);
		this.snatchData("A15050C", dao, cn_names);
	}

	// 抓取
	// public static void main(String[] args) throws Exception {
	public void startCrawl() throws Exception {

		// 从数据库取任务
		List<String> tag1 = demoSql(dao, "tag1");

		List<String> tag2 = demoSql(dao, "tag2");

		List<String> tag3 = demoSql(dao, "tag3");

		List<String> cn_names = demoSql(dao, "cn_name");

		String html;
		String html1;
		String html2;

		try {
			Map<String, String> headers = new HashMap<String, String>();
			headers.put("Host", "data.stats.gov.cn");
			headers.put("Origin", "http://data.stats.gov.cn");
			headers.put("Referer", "http://data.stats.gov.cn/easyquery.htm?cn=A01");
			Map<String, String> data2 = new HashMap<String, String>();
			data2.put("id", "zb");
			data2.put("dbcode", "hgyd");
			data2.put("wdcode", "zb");
			data2.put("m", "getTree");
			html = new HttpBase().post("http://data.stats.gov.cn/easyquery.htm", "utf-8", headers, data2);
			html = "{'data':" + html + "}";
			JSONObject c = JSON.parseObject(html);
			JSONArray c10 = c.getJSONArray("data");
			for (int k = 0; k < c10.size(); k++) {
				JSONObject c1010 = c10.getJSONObject(k);
				String pid1 = c1010.getString("id");
				String sub2 = c1010.getString("name");
				// 筛选条件
				if (!tag1.contains(sub2)) {
					continue;
				}
				Map<String, String> data = new HashMap<String, String>();
				data.put("id", pid1);
				data.put("dbcode", "hgyd");
				data.put("wdcode", "zb");
				data.put("m", "getTree");
				html1 = new HttpBase().post("http://data.stats.gov.cn/easyquery.htm", "utf-8", headers, data);
				html1 = "{'data':" + html1 + "}";
				JSONObject a = JSON.parseObject(html1);
				JSONArray a10 = a.getJSONArray("data");
				for (int i = 0; i < a10.size(); i++) {
					JSONObject a1010 = a10.getJSONObject(i);
					String pid = a1010.getString("id");
					String sub3 = a1010.getString("name");
					// 筛选条件
					if (!tag2.contains(sub3)) {
						continue;
					}
					Map<String, String> data1 = new HashMap<String, String>();
					data1.put("id", pid);
					data1.put("dbcode", "hgyd");
					data1.put("wdcode", "zb");
					data1.put("m", "getTree");
					html2 = new HttpBase().post("http://data.stats.gov.cn/easyquery.htm", "utf-8", headers, data1);
					if (html2.equals("[]")) {
						snatchData(pid, dao, cn_names);
					} else {
						html2 = "{'data':" + html2 + "}";
						JSONObject b = JSON.parseObject(html2);
						JSONArray b10 = b.getJSONArray("data");
						for (int j = 0; j < b10.size(); j++) {
							JSONObject b1010 = b10.getJSONObject(j);
							String pid2 = b1010.getString("id");
							String sub4 = b1010.getString("name");
							// 筛选条件
							if (!tag3.contains(sub4)) {
								continue;
							}
							// 存入数据库
							snatchData(pid2, dao, cn_names);
						}
					}
				}

			}

		} catch (Exception e) {

			e.printStackTrace();
		}

	}

	public  void snatchData(String pid, Dao dao, List<String> cn_names) throws Exception {
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Host", "data.stats.gov.cn");
		headers.put("Referer", "http://data.stats.gov.cn/easyquery.htm?cn=A01");
		headers.put("User-Agent",
				"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
		String url = "http://data.stats.gov.cn/easyquery.htm?m=QueryData&dbcode=hgyd&rowcode=zb&colcode=sj&wds=%5B%5D&dfwds=%5B%7B%22wdcode%22%3A%22zb%22%2C%22valuecode%22%3A%22"
				+ pid + "%22%7D%5D&k1=" + new Date().getTime();
		
		//headers.put("cookie", "_trs_uv=jd1hcdms_6_5v09; JSESSIONID=564BD0E6976A5CAE626ED466F308E18C; u=5");

		// 陶宝的老方法 一个月抓一次用
		String html3 = new HttpBase().get(url, "utf-8", headers);

		JSONObject d = JSON.parseObject(html3);
		// System.out.println(html3);
		JSONArray d10 = d.getJSONObject("returndata").getJSONArray("wdnodes").getJSONObject(0).getJSONArray("nodes");
		for (int p = 0; p < d10.size(); p++) {
			JSONObject e1010 = d10.getJSONObject(p);
			String code = e1010.getString("code");
			String sub = e1010.getString("name");
			// 筛选条件 (按名字)
			if (!cn_names.contains(sub)) {
				continue;
			}
			JSONArray f10 = d.getJSONObject("returndata").getJSONArray("datanodes");
			for (int o = 0; o < f10.size(); o++) {
				JSONObject f1010 = f10.getJSONObject(o);
				String code1 = f1010.getJSONArray("wds").getJSONObject(0).getString("valuecode");
				String date = f1010.getJSONArray("wds").getJSONObject(1).getString("valuecode");
				if (code1.equals(code)) {

					PropIndex prop = dao.fetch(PropIndex.class, Cnd.where("cn_name", "=", sub));

					String strdata = f1010.getJSONObject("data").getString("strdata");

					MonthData tmd = new MonthData();
					tmd.setCn_name(sub);
					tmd.setCreate_time(new Date());
					tmd.setData(strdata);
					tmd.setEn_name(prop.getEn_name());
					tmd.setYm(Integer.parseInt(date));
					tmd.setProp(prop.getProp());

					try {
						// dao.fastInsert(gi);
						dao.fastInsert(tmd);
						logger.info(sub + "," + strdata + "," + date);
					} catch (DaoException e) {
						// 报错就更新 (一般是重复异常)
						MonthData mm = dao.fetch(MonthData.class,
								Cnd.where("cn_name", "=", sub).and("ym", "=", Integer.parseInt(date)));
						if (mm == null) {
							logger.info("月度为空,:" + sub + "," + strdata);
						}
						if (StringUtils.isNotEmpty(strdata)) {
							if (mm.getData() != null) {
								if (!strdata.equals(mm.getData())) {
									dao.update(MonthData.class,
											Chain.make("data", strdata).add("update_time", new Date()),
											Cnd.where("cn_name", "=", sub).and("ym", "=", Integer.parseInt(date)));
								} else {
									logger.info("数据相同不更新,:" + sub + "," + strdata);
								}
							} else {
								dao.update(MonthData.class, Chain.make("data", strdata).add("update_time", new Date()),
										Cnd.where("cn_name", "=", sub).and("ym", "=", Integer.parseInt(date)));
								logger.info("更新:" + sub + "," + strdata);
							}

						}

					} catch (Exception e) {
						// 别的错误
						logger.error("", e);
					}

				}
			}
		}
	}

}
